/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-05-27
*/
package com.rzico.order.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.core.entity.SysMch;
import com.rzico.core.service.SysMchService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.order.entity.Askfor;
import com.rzico.order.model.DistributionDetailVo;
import com.rzico.order.model.DistributionListVo;
import com.rzico.order.model.DistributionVo;
import com.rzico.order.service.DistributionVoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;

/**
 * 账单接口
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-05-27
 */
@Api(description = "账单接口")
@RestController
@RequestMapping("/admin/distributionVo")
public class DistributionVoAdminController extends BaseController {

    @Autowired
    private DistributionVoService distributionVoService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private SysMchService sysMchService;

    /**
     * 查询账单记录
     *
     * @return
     */
    @ApiOperation("查询账单记录")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseId", value = "商户企业ID", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "distributionDate", value = "结算日期", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态（0.未结算，1.已结算）", dataType = "String", paramType = "query")
    })
    public CommResult<DistributionVo> list(Long enterpriseId,Date startDate, Date endDate,Date distributionDate,String status, Pageable pageable) {

        Map<String,Object> params = new HashMap<>();
        if (startDate!=null){
            params.put("startDate", org.apache.commons.lang3.time.DateUtils.truncate(startDate, Calendar.DATE));
        }
        if (endDate!=null){
            params.put("endDate", org.apache.commons.lang3.time.DateUtils.addMilliseconds(org.apache.commons.lang3.time.DateUtils.addDays(endDate,1),-1));
        }

        if ("0".equals(status)){
            params.put("isDistribution", 0);
        }

        if ("1".equals(status)){
            params.put("isDistribution", 1);
            params.put("orderStatus",2);
        }

        if (distributionDate!=null) {
            params.put("distributionDate", distributionDate);
        }

        if (enterpriseId!=null) {
            params.put("enterpriseId", enterpriseId);
        } else {
            Enterprise enterprise = enterpriseService.getCurrent();
            SysMch sysMch = sysMchService.selectByPrimaryKey(enterprise.getMchId());
            if (!sysMch.getMchType().equals(0)) {
                if (enterprise != null) {
                    params.put("enterpriseId", enterprise.getId());
                } else {
                    return CommResult.error("没有开通企业");
                }
            }
        }
        params.put("sortType","desc");

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<DistributionVo> list = distributionVoService.selectDistributionVoList(params);
        PageResult<DistributionVo> pageResult = new PageResult<DistributionVo>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);

    }


    /**
     * 查询账单详情汇总
     *
     * @return
     */
    @ApiOperation("查询账单详情汇总")
    @GetMapping("/detailView")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseId", value = "商户企业ID", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "orderDate", value = "订单日期", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "distributionDate", value = "结算日期", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态（0.未结算，1.已结算）", dataType = "String", paramType = "query")
    })
    public CommResult<DistributionVo> detailView(Long enterpriseId,Date orderDate,Date distributionDate, String status) {

        Map<String,Object> params = new HashMap<>();
        if (orderDate!=null){
            params.put("startDate", org.apache.commons.lang3.time.DateUtils.truncate(orderDate, Calendar.DATE));
        }
        if (orderDate!=null){
            params.put("endDate", org.apache.commons.lang3.time.DateUtils.addMilliseconds(org.apache.commons.lang3.time.DateUtils.addDays(orderDate,1),-1));
        }
        if ("0".equals(status)){
            params.put("isDistribution", 0);
        }
        if ("1".equals(status)){
            params.put("isDistribution", 1);

            params.put("orderStatus",2);
        }

        if (distributionDate!=null) {
            params.put("distributionDate", distributionDate);
        }

        if (enterpriseId!=null) {
            params.put("enterpriseId", enterpriseId);
        } else {
            Enterprise enterprise = enterpriseService.getCurrent();
            SysMch sysMch = sysMchService.selectByPrimaryKey(enterprise.getMchId());
            if (!sysMch.getMchType().equals(0)) {
                if (enterprise != null) {
                    params.put("enterpriseId", enterprise.getId());
                } else {
                    return CommResult.error("没有开通企业");
                }
            }
        }

        params.put("sortType","desc");
        List<DistributionDetailVo> list = distributionVoService.selectDistributionDetailVo(params);
        if (list.size()>0) {
            return CommResult.success(list.get(0));
        } else {
            return CommResult.success();
        }

    }


    /**
     * 查询账单订单记录
     *
     * @return
     */
    @ApiOperation("查询账单订单记录")
    @GetMapping("/orderList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseId", value = "商户企业ID", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "orderDate", value = "查询时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "distributionDate", value = "结算日期", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态（0.未结算，1.已结算）", dataType = "String", paramType = "query")
    })
    public CommResult<DistributionVo> orderList(Long enterpriseId,Date orderDate,Date distributionDate, String status, Pageable pageable) {

        Map<String,Object> params = new HashMap<>();
        if (orderDate!=null){
            params.put("startDate", org.apache.commons.lang3.time.DateUtils.truncate(orderDate, Calendar.DATE));
        }
        if (orderDate!=null){
            params.put("endDate", org.apache.commons.lang3.time.DateUtils.addMilliseconds(org.apache.commons.lang3.time.DateUtils.addDays(orderDate,1),-1));
        }
        if ("0".equals(status)){
            params.put("isDistribution", 0);
        }
        if ("1".equals(status)){
            params.put("isDistribution", 1);
            params.put("orderStatus",2);
        }

        if (distributionDate!=null) {
            params.put("distributionDate", distributionDate);
        }

        if (enterpriseId!=null) {
            params.put("enterpriseId", enterpriseId);
        } else {
            Enterprise enterprise = enterpriseService.getCurrent();
            SysMch sysMch = sysMchService.selectByPrimaryKey(enterprise.getMchId());
            if (!sysMch.getMchType().equals(0)) {
                if (enterprise != null) {
                    params.put("enterpriseId", enterprise.getId());
                } else {
                    return CommResult.error("没有开通企业");
                }
            }
        }
        params.put("sortType","desc");

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<DistributionListVo> list = distributionVoService.selectDistributionListVo(params);
        PageResult<DistributionListVo> pageResult = new PageResult<DistributionListVo>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);

    }

    /**
     * 查询当天账单状态
     *
     * @return
    */
    @ApiOperation("查询当天账单状态")
    @GetMapping("/summaryForToday")
    public CommResult<DistributionVo> summaryForToday() {

        Map<String,Object> data = new HashMap<>();

        Map<String,Object> params = new HashMap<>();
        params.put("startDate", org.apache.commons.lang3.time.DateUtils.truncate(new Date(), Calendar.DATE));

        params.put("endDate", org.apache.commons.lang3.time.DateUtils.addMilliseconds(org.apache.commons.lang3.time.DateUtils.addDays(new Date(),1),-1));


        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }

        params.put("orderStatus", 1);

        BigDecimal waitEnd = BigDecimal.ZERO;
        BigDecimal ended = BigDecimal.ZERO;
        BigDecimal waitDist = BigDecimal.ZERO;

        List<DistributionVo> wlist = distributionVoService.selectDistributionVoList(params);
        for (DistributionVo vo:wlist) {
            if (vo!=null) {
                waitEnd = waitEnd.add(vo.getDistributionAmount());
            }
        }
        params.put("orderStatus", 2);
        List<DistributionVo> elist = distributionVoService.selectDistributionVoList(params);
        for (DistributionVo vo:elist) {
            if (vo!=null) {
                ended = ended.add(vo.getDistributionAmount());
            }
        }

        params.clear();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }
        params.put("isDistribution", 0);
 //       params.put("orderStatus",2);
        List<DistributionVo> dlist = distributionVoService.selectDistributionVoList(params);
        for (DistributionVo vo:dlist) {
            if (vo!=null) {
                waitDist = waitDist.add(vo.getDistributionAmount());
            }
        }

        data.put("waitEnd",waitEnd);
        data.put("ended",ended);
        data.put("waitDist",waitDist);

        return CommResult.success(data);

    }

    /**
     * 结账测试
     *
     * @return
     */
    @ApiOperation("结账测试")
    @GetMapping("/jobStart")
    public CommResult<DistributionVo> jobStart() {
        distributionVoService.jobStart();
        return CommResult.success();
    }

}
